﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Siemens.Engineering;
using Siemens.Engineering.HW;
using Siemens.Engineering.HW.Features;
using Siemens.Engineering.SW;
using Siemens.Engineering.SW.Blocks;
using Siemens.Engineering.SW.ExternalSources;
using Siemens.Engineering.SW.Tags;
using Siemens.Engineering.SW.Types;
using Siemens.Engineering.Hmi;
using HmiTarget = Siemens.Engineering.Hmi.HmiTarget;
using Siemens.Engineering.Hmi.Tag;
using Siemens.Engineering.Hmi.Screen;
using Siemens.Engineering.Hmi.Cycle;
using Siemens.Engineering.Hmi.Communication;
using Siemens.Engineering.Hmi.Globalization;
using Siemens.Engineering.Hmi.TextGraphicList;
using Siemens.Engineering.Hmi.RuntimeScripting;
using Siemens.Engineering.Compiler;
using Siemens.Engineering.Library;
using System.IO;
namespace TIA_Openness
{
    internal class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Starting TIA Portal");
            using (TiaPortal tiaPortal = new TiaPortal(TiaPortalMode.WithoutUserInterface)) //打开博途
            {
                Console.WriteLine("TIA Portal has started");
                ProjectComposition projects = tiaPortal.Projects;
                Console.WriteLine("Opening Project...");
                // please adapt the path and the extension apx to the installed version of TIA Portal
                //项目文件路径
                FileInfo projectPath = new FileInfo("D:\\A2A22001A&A2B22082A-FCC EOL& FA\\程序\\A2B22082A\\A2B22082A.ap15_1");
                
                Project _project = null;

                //打开项目
                try
                {
                    _project = projects.OpenWithUpgrade(projectPath);
                }
                catch (Exception)
                {
                    Console.WriteLine(String.Format("Could not open project {0}", projectPath.FullName));
                    Console.WriteLine("Demo complete hit enter to exit");
                    Console.ReadLine();
                }
                //打开完成
                Console.WriteLine(String.Format("Project {0} is open", _project.Path.FullName));

                //新建PLC
                //DeviceComposition devices = _project.Devices;
                //Device newDevice = devices.CreateWithItem(@"OrderNumber:6ES7 511-1AK02-0AB0/V2.6", "PLC_11", "NewDevice");

                //枚举设备
                GetDevices.EnumerateDevicesInGroups(_project);
                foreach (Device item in GetDevices.list)
                {
                    Console.WriteLine(item.Name);
                }
                //所有设备存入 GetDevices.list 列表中
                //获取属性
                Device device = GetDevices.list[1];

                #region 获取属性
                IList<string> attributeNames = new List<string>();
                IList<EngineeringAttributeInfo> attributes = ((IEngineeringObject)device).GetAttributeInfos();
                foreach (EngineeringAttributeInfo engineeringAttributeInfo in attributes)
                {
                    string name = engineeringAttributeInfo.Name;
                    attributeNames.Add(name);
                }
                IList<object> values = ((IEngineeringObject)device).GetAttributes(attributeNames);
                for (int i = 0; i < attributes.Count; i++)
                {
                    Console.WriteLine("attribute name: " + attributeNames[i] + " value: " + values[i]);
                } 
                #endregion

                //获取device组成设备名
                foreach (var item in device.DeviceItems)
                {
                    Console.WriteLine(item.Name);
                }
                
                //获取HmiTatget
                HmiTarget hmiTarget = GetHmiTarget.GetHmiTargetFromDevice(device);

                //导出变量表
                //string pathTag = @"D:\A2A22001A&A2B22082A-FCC EOL& FA\程序\变量表";
                //HMI_TagTable_Helper.ExportAllTagTablesFromHMITarget(hmiTarget,path);

                //导出所有画面
                //string pathScreen = @"D:\A2A22001A&A2B22082A-FCC EOL& FA\程序\画面";
                //ExportScreensHelper.ExportScreensOfDevice(pathScreen, hmiTarget);

                //导入画面
                FileInfo screenFileInfo = new FileInfo(@"C:\Users\Snody\Desktop\测试\Screens\画面_IN_1.xml");
                ////ScreenUserFolder folder = hmiTarget.ScreenFolder.Folders.Create("MyFolder");
                ////folder.Screens.Import(screenFileInfo, ImportOptions.Override);
                hmiTarget.ScreenFolder.Screens.Import(screenFileInfo, ImportOptions.Override);

                _project.Save();
                Console.WriteLine("Done");
                //Console.ReadKey();

                _project.Close();
                Console.WriteLine("Demo complete hit enter to exit");
                Console.ReadLine();
            }
        }
      

    }


}
